//
//  BSWidget.m
//  share_business
//
//  Created by 吴于凌 on 2017/8/26.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "BSWidget.h"
#import "UIView+Extension.h"

@implementation BSWidget

//创建UILabel
+ (UILabel *)commonLableWithFrame:(CGRect)frame
                             text:(NSString *)text
                            color:(UIColor *)color
                             font:(float)fontSize

{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    if (color) {
        label.textColor = color;
    }
    label.font = [UIFont systemFontOfSize:fontSize];
    label.backgroundColor = [UIColor clearColor];
    return label;
}

//创建UITextField
+ (UITextField *)commonTextFieldWithFrame:(CGRect)frame
                              placeholder:(NSString *)placeholder
                                    color:(UIColor *)color
                                     font:(UIFont *)font
                          secureTextEntry:(BOOL)secureTextEntry
                                 delegate:(id)delegate
{
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    textField.placeholder = placeholder;
    textField.textColor = color;
    textField.font = font;
    textField.secureTextEntry = secureTextEntry;
    textField.delegate = delegate;
    textField.clearsOnBeginEditing = NO ;
    return textField;
}

//创建UITextView
+ (UITextView *)commonTextViewWithFrame:(CGRect)frame
                                   text:(NSString *)text
                                  color:(UIColor *)color
                                   font:(UIFont *)font
                          textAlignment:(NSTextAlignment)textAlignment
{
    UITextView *textView = [[UITextView alloc] initWithFrame:frame];
    textView.text = text;
    textView.textColor = color;
    textView.textAlignment = textAlignment;
    
    textView.backgroundColor = [UIColor clearColor];
    //    textView.editable = NO;
    textView.scrollEnabled = NO;
    textView.dataDetectorTypes = UIDataDetectorTypeLink;
    
    return textView;
}

//创建UIButton
+ (UIButton *)commonButtonWithFrame:(CGRect)frame
                              title:(NSString *)title
                              color:(UIColor *)color
                           fontSize:(float)fontSize
                             target:(id)target
                             action:(SEL)action
{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    if (color) {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    [btn.titleLabel setFont:[UIFont systemFontOfSize:fontSize]];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}

//创建图片
+ (UIImageView*) commonImageViewWithFrame:(CGRect)frame
                                    image:(UIImage*)image
{
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.image = image;
    return imgView;
}


//返回颜色图片
+ (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size {
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}
//设置右上角角标
+(void)addJSBadgeView:(UIView *)view title:(NSString *)title{
    if (!title) {
        return ;
    }
    JSBadgeView  *badgeView  = [[JSBadgeView alloc ] initWithParentView:view alignment:JSBadgeViewAlignmentTopRight];
    [badgeView setBadgeText:title];
    
    badgeView.badgePositionAdjustment = CGPointMake(-5,5);
    [badgeView setSize:CGSizeMake(30,30) ];
    [view  addSubview:badgeView];
    [badgeView sendSubviewToBack:view];
}

//设置右上角角标
+(void)addJSBadgeViewSmall:(UIView *)view title:(NSString *)title{
    if (!title) {
        return ;
    }
    JSBadgeView  *badgeView  = [[JSBadgeView alloc ] initWithParentView:view alignment:JSBadgeViewAlignmentTopRight];
    [badgeView setBadgeText:title];
    badgeView.badgeTextFont = [UIFont systemFontOfSize:8];
    [view  addSubview:badgeView];
    [badgeView sendSubviewToBack:view];
}

//设置右上角角标
+(void)addJSBadgeViewWithY:(UIView *)view title:(NSString *)title y:(float)y{
    if (!title) {
        return ;
    }
    JSBadgeView  *badgeView  = [[JSBadgeView alloc ] initWithParentView:view alignment:JSBadgeViewAlignmentTopRight];
    [badgeView setBadgeText:title];
    
    badgeView.badgePositionAdjustment = CGPointMake(-5,y);
    [badgeView setSize:CGSizeMake(30,30) ];
    [view  addSubview:badgeView];
    [badgeView sendSubviewToBack:view];
}


@end
